package com.ycy.temperature.view.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.jude.utils.JUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.ycy.temperature.R;
import com.ycy.temperature.bean.VerificationBean;
import com.ycy.temperature.date.net.UserNetWorks;
import com.ycy.temperature.view.base.BaseActivity;
import com.ycy.temperature.view.fragment.CircleFragment;
import com.ycy.temperature.view.fragment.HomePageFragment;
import com.ycy.temperature.view.fragment.MeFragment;
import com.ycy.temperature.view.fragment.NewsFragment;
import com.ycy.temperature.view.fragment.ReleaseFragment;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, PopupWindow.OnDismissListener {


    @BindView(R.id.fl)
    FrameLayout fl;
    @BindView(R.id.nihao)
    View nihao;
    @BindView(R.id.rd_analysis)
    RadioButton rdAnalysis;
    @BindView(R.id.rd_educationadmin)
    RadioButton rdEducationadmin;
    @BindView(R.id.rd_daily)
    RadioButton rdDaily;
    @BindView(R.id.rd_finance)
    RadioButton rdFinance;
    @BindView(R.id.rd_me)
    RadioButton rdMe;
    @BindView(R.id.rg_oper)
    RadioGroup rgOper;
    @BindView(R.id.img_protruding)
    ImageView imgProtruding;

    PopupWindow popupWindow;
    int navigationHeight;

    FragmentTransaction transaction1;
    FragmentManager fm;
    HomePageFragment homePageFragment;
    CircleFragment circleFragment;
    NewsFragment newsFragment;
    ReleaseFragment releaseFragment;
    MeFragment meFragment;

    AutoLinearLayout LayoutActivity;
    AutoLinearLayout LayoutDynamic;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void setData() {
//        getMe();
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        rgOper.setOnCheckedChangeListener(this);
        fm = getSupportFragmentManager();
        homePageFragment = (HomePageFragment) fm.findFragmentByTag("homePageFragment");
        circleFragment = (CircleFragment) fm.findFragmentByTag("circleFragment");
        newsFragment = (NewsFragment) fm.findFragmentByTag("newsFragment");
        releaseFragment = (ReleaseFragment) fm.findFragmentByTag("releaseFragment");
        meFragment = (MeFragment) fm.findFragmentByTag("meFragment");

        transaction1 = fm.beginTransaction();
        homePageFragment = new HomePageFragment();
        transaction1.add(R.id.fl, homePageFragment, "homePageFragment").commit();
        changeImageSize();

        rdDaily.setClickable(false);
        rdDaily.setEnabled(false);
        int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        navigationHeight = getResources().getDimensionPixelSize(resourceId);
    }

    private void changeImageSize() {
        //定义底部标签图片大小
        Drawable rdAnalysis = getResources().getDrawable(R.drawable.operationmanage_selector);
        rdAnalysis.setBounds(0, 0, 70, 65);//第一距左右边距离，第二距上下边距离，第三宽度,第四高度
        this.rdAnalysis.setCompoundDrawables(null, rdAnalysis, null, null);//只放上面

        Drawable rdEducationadmin = getResources().getDrawable(R.drawable.operationmanage_daily_selector);
        rdEducationadmin.setBounds(0, 0, 70, 65);//第一距左右边距离，第二距上下边距离，第三宽度,第四高度
        this.rdEducationadmin.setCompoundDrawables(null, rdEducationadmin, null, null);//只放上面

        Drawable rdDaily = getResources().getDrawable(R.drawable.operationmanage_edu_selector);
        rdDaily.setBounds(0, 0, 70, 65);//第一距左右边距离，第二距上下边距离，第三宽度,第四高度
        this.rdDaily.setCompoundDrawables(null, rdDaily, null, null);//只放上面

        Drawable rdFinance = getResources().getDrawable(R.drawable.operationmanage_fiance_selector);
        rdFinance.setBounds(0, 0, 70, 65);//第一距左右边距离，第二距上下边距离，第三宽度,第四高度
        this.rdFinance.setCompoundDrawables(null, rdFinance, null, null);//只放上面

        Drawable rdMe = getResources().getDrawable(R.drawable.operationmanage_me_selector);
        rdMe.setBounds(0, 0, 70, 65);//第一距左右边距离，第二距上下边距离，第三宽度,第四高度
        this.rdMe.setCompoundDrawables(null, rdMe, null, null);//只放上面
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        transaction1 = fm.beginTransaction();
        if (homePageFragment != null) {
            transaction1.hide(homePageFragment);
        }
        if (circleFragment != null) {
            transaction1.hide(circleFragment);
        }
        if (newsFragment != null) {
            transaction1.hide(newsFragment);
        }
//        if (releaseFragment != null) {
//            transaction1.hide(releaseFragment);
//        }
        if (meFragment != null) {
            transaction1.hide(meFragment);
        }
        if (checkedId == R.id.rd_analysis) {
            if (homePageFragment == null) {
                homePageFragment = new HomePageFragment();
                transaction1.add(R.id.fl, homePageFragment, "homePageFragment");
            } else {
                transaction1.show(homePageFragment);
            }
            imgProtruding.setImageResource(R.mipmap.image_release);

        } else if (checkedId == R.id.rd_educationadmin) {
            if (circleFragment == null) {
                circleFragment = new CircleFragment();
                transaction1.add(R.id.fl, circleFragment, "mEducationalAdminFragment");
            } else {
                transaction1.show(circleFragment);
            }
            imgProtruding.setImageResource(R.mipmap.image_release);

//        } else if (checkedId == R.id.rd_daily) {
//            if (releaseFragment == null) {
//                releaseFragment = new ReleaseFragment();
//                transaction1.add(R.id.fl, releaseFragment, "mDailyFragment");
//            } else {
//                transaction1.show(releaseFragment);
//            }
//            imgProtruding.setImageResource(R.mipmap.image_release);
//
        } else if (checkedId == R.id.rd_finance) {
            if (newsFragment == null) {
                newsFragment = new NewsFragment();
                transaction1.add(R.id.fl, newsFragment, "mFinanceFragment");
            } else {
                transaction1.show(newsFragment);
            }
            imgProtruding.setImageResource(R.mipmap.image_release);

        } else if (checkedId == R.id.rd_me) {
            if (meFragment == null) {
                meFragment = new MeFragment();
                transaction1.add(R.id.fl, meFragment, "mMeFragment");
            } else {
                transaction1.show(meFragment);
            }
            imgProtruding.setImageResource(R.mipmap.image_release);

        }
        transaction1.commit();
    }

    @OnClick({R.id.img_protruding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_protruding:
                openPopupWindow(view);
                break;
        }
    }


    private void openPopupWindow(View v) {
        //防止重复按按钮
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        //设置PopupWindow的View
        View view = LayoutInflater.from(this).inflate(R.layout.view_popupwindow, null);
        LayoutActivity = view.findViewById(R.id.Layout_Activity);
        LayoutDynamic = view.findViewById(R.id.Layout_Dynamic);
        LayoutActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getJudgeRelease();
            }
        });
        LayoutDynamic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, DynamicActivity.class));
                popupWindow.dismiss();
            }
        });
        popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        //设置动画
        popupWindow.setAnimationStyle(R.style.PopupWindow);
        //设置位置
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, navigationHeight);
        //设置消失监听
        popupWindow.setOnDismissListener(this);
        //设置背景色
        setBackgroundAlpha(0.8f);
    }

    //设置屏幕背景透明效果
    public void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = alpha;
        getWindow().setAttributes(lp);
    }

    @Override
    public void onDismiss() {
        setBackgroundAlpha(1);
        popupWindow.dismiss();
    }

    public void getJudgeRelease() {
        UserNetWorks.getJudgeRelease("0", new Subscriber<VerificationBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(VerificationBean verificationBean) {
                if (verificationBean.getResponseState().equals("1")) {
                    startActivity(new Intent(MainActivity.this, ActivityActivity.class));
                    popupWindow.dismiss();
                } else {
                    JUtils.Toast(verificationBean.getMsg());
                }
            }
        });
    }
}
